Send Emails With Node.js. In this article, I will discuss sending e-mail with Node.js. I’ve covered Express.js tutorials and I will also use NodeMailer in this article. For the verification, password recovery and promotion of account, many forums and blogs asked people about the sending of emails with Node.js. So let’s start with a tutorial and at the end there is a node app that can send e-mails to any account.
Nodemailer is a Node.js module for easy to send e-mail like cake. The project started in 2010, when it wasn’t possible to send E-mails, today it’s the default solution for the majority of Node.js users.
Step -1: You need to install NodeMailer package to your node application
npm install --save nodemailer
Step-2: You need to require nodeMailer in your route file
const nodeMailer = require('nodemailer');
Now, use the standard SMTP transport to generate transporter object that can send messages. In this example, I will use gmail SMTP to send Emails With Node.js
Step-3: Create transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: ‘gmail’,
host: ‘smtp.gmail.com’,
secure: ‘true’,
port: ‘465’,
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
});
In auth object replace details with your email address and password.
Step-4: We now need to configure the information of our email.
// setup email data
let mailOptions = {
from: '"Your Name" ', // sender address
to: 'myfriend@gmail.com', // list of receiver
subject: 'Sending Email using Node.js', // Subject line
text: 'Hello world', // plain text body
};
At the last we will ‘sendMail()’ method provided by the transporter object we created to send the email.
To send HTML formatted text in your email, use the “html” property instead of the “text” property:
// setup email data
let mailOptions = {
from: '"Your Name" ', // sender address
to: 'myfriend@gmail.com', // list of receiver
subject: 'Sending Email using Node.js', // Subject line
html: 'Hello World
That was easy!
' // html body
};
To send an email to more than one users, add them to the “to” property of the mailOptions object, separated by commas:
to: ‘myfriend@gmail.com, myotherfriend@gmail.com‘,
Step-5: Sending the email using ‘sendMail()’ method provided by the transporter object
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
‘sendMail()’ method requires two argument mailOptions and a callback function which will be called when the mail is sent. The callback function will be called when either email sent successfully or an error occurred.
Complete Code: send Emails With Node.js
var nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: ‘gmail’,
host: ‘smtp.gmail.com’,
secure: ‘true’,
port: ‘465’,
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
});
let mailOptions = {
from: '"Your Name" ', // sender address
to: 'myfriend@gmail.com, // list of receivers
subject: 'Sending Email using Node.js', // Subject line
text: 'Hello world', // plain text body
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
How to Create Pagination with Node.js, MongoDB, Express and EJS Step by Step
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co
